<?php
	class Firmware extends MY_Controller{
	
		public $_msg 			= array();
		public $folder_upload	= "./download/";			// for uploading
		
		public function __construct(){
			parent::__construct();
			$this->load->helper(array('form', 'url'));
		}
		
		public function index(){
			$data =array();
			$data['f'] = $this->list_file();
			$data['title'] = "Firmware";
			$this->load->view('templates/header1', $data);
			$this->load->view('firmware/index', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');			
		}
		
		public function list_file(){
			
			$data = array();
			if (is_dir($this->folder_upload)) {
				if ($dh = opendir($this->folder_upload)) {
					while (($file = readdir($dh)) !== false) {
						if($file!=".." && $file!="." && $file!="apasswords" && $file!=".htaccess"){
							$data['f'] = $file; 
						}
					}
				closedir($dh);
				}
			}
			return $data;
		}
		
		public function create(){
			$data['title'] = "Firmware upload";
			$data['error'] = "";
			$this->load->view('templates/header1', $data);
			$this->load->view('firmware/form', $data);
			$this->load->view('templates/right', $data);
			$this->load->view('templates/footer');
		}	
		
		public function save(){			
			/*$config['upload_path'] 		= $this->folder_upload;
			$config['allowed_types'] 	= 'bin|txt|pdf';								
			$this->load->library('upload', $config);
			
			$data['title'] = "Firmware upload";
			if (!$this->upload->do_upload('firmware')){		
				echo "Falied:";
				echo "<pre>";
				print_r($this->upload->data());
				print_r($_FILES);
				echo "</pre>";
				exit();
				$data['error'] = $this->upload->display_errors();
				$this->load->view('templates/header1', $data);
				$this->load->view('firmware/form', $data);
				$this->load->view('templates/footer');
			}
			else{
				echo "Success:";
				echo "<pre>";
				print_r($this->upload->data());
				print_r($_FILES);
				echo "</pre>";
				exit();
				$data['upload_data'] = $this->upload->data();
				$this->load->view('templates/header1', $data);
				$this->load->view('firmware/upload_success', $data);
				$this->load->view('templates/footer');
			}*/
			// Upload library has the issues in version 2.1.0. It's not stable
			// Using our function
			
			if($this->my_upload('firmware')===FALSE){
				$data['title'] = "Firmware upload";
				$data['error'] = $this->_msg[0];
				$this->load->view('templates/header1', $data);
				$this->load->view('firmware/form', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
			else{
				$data['title'] = "Firmware upload";
				$this->load->view('templates/header1', $data);
				$this->load->view('firmware/upload_success', $data);
				$this->load->view('templates/right', $data);
				$this->load->view('templates/footer');
			}
		}
		
		public function download($f){
			$this->load->helper('download');
			// to download the file existing on the server, need to read in into string
			$data = file_get_contents($this->folder_upload.$f);
			$name = $f;
			force_download($name, $data);
		}
		
		public function delete($f=FALSE){
			if($f!=FALSE){
				$path  = $this->folder_upload.$f;
				if(is_file($path) && file_exists($path)){
					unlink($path);					
				}
				redirect('firmware', 'refresh');
			}
			else return false;
		}
		
		public function my_upload($f){
			$mime_type = 'application/octet-stream';
			$file_type = substr($_FILES[$f]['name'], strrpos($_FILES[$f]['name'], "."), strlen($_FILES[$f]['name']));
			$flag = TRUE;
			if($file_type!=".bin" || $_FILES[$f]['type']!=$mime_type){
				$this->_msg[] = "File type attemp is not allowed";
				$flag = FALSE;
			}
			if($flag && !move_uploaded_file($_FILES[$f]["tmp_name"], $this->folder_upload . $_FILES[$f]["name"])){
				$this->_msg[] = "Error occurs when uploading file";
				$flag = FALSE;
			}
			return $flag;
		}
		
	}
?>